//
//  YMListView.swift
//  YMSwiftUI
//
//  Created by anscen on 2022/3/18.
//

import SwiftUI
import Alamofire

struct YMListView: View {
    @State var group  :String? = nil
    @State var name   :String? = nil
    
    @State private var listAdap :[GridItem] =  [GridItem(.adaptive(minimum: list_width, maximum: list_width + 10), spacing: 0, alignment: .center)]
    @State private var listData :[YMBook] = []
    @State private var refreshData = BaseRefreshData()
    func loadRefreshAction(){
        self.loadData(1)
    }
    func loadMoreAction(){
        self.loadData(refreshData.page)
    }
    var body: some View {
        GeometryReader.init { rootSize in
            VStack(alignment: .leading, spacing: 0) {
                if listData.count == 0{
                    BaseRefreshView(refreshing: $refreshData.refreshHeader)
                }else{
                    ScrollView(.vertical, showsIndicators: false){
                        RefreshHeader(refreshing: $refreshData.refreshHeader) {
                            self.loadRefreshAction()
                        } label: { progess in
                            BaseRefreshHeaderView(refreshing: refreshData.refreshHeader)
                        }
                        LazyVGrid(columns: listAdap, spacing: 0){
                            ForEach(listData, id: \.self){
                                item in
                                YMListCell(book: item,width: rootSize.size.width)
                            }
                        }
                        RefreshFooter(refreshing: $refreshData.refreshFooter, action: loadMoreAction) {
                            BaseRefreshFooterView(noMoreData: refreshData.noMoreData,refreshing: refreshData.refreshFooter)
                        }.preload(offset: 50).noMore(refreshData.noMoreData)
                    }.enableRefresh()
                }
            }.onAppear {
                self.refreshData.refreshHeader = true
                self.loadRefreshAction()
            }.onDisappear {

            }.navigationBarHidden(false).navigationBarBackButtonHidden(true).navigationBarItems(leading: BaseNavigationBarBackButton())
        }
    }
    func loadData(_ page :Int){
        debugPrint(refreshData.refreshHeader)
        if let name = name,let group = group{
            ApiMoya.request(target: .classifyTail(group, name: name, page: page)) { json in
                if page == 1{
                    self.listData.removeAll()
                }
                guard let list = [YMBook].deserialize(from: json["books"].rawString()) else { return }
                if let data = list as? [YMBook]{
                    refreshData.page = refreshData.page + 1
                    self.listData.append(contentsOf: data)
                }
                refreshData.endRefresh(list.count == 0)
            } failure: { error in
                refreshData.endFailure(error)
            }
        }
    }
}

//struct YMListView_Previews: PreviewProvider {
//    static var previews: some View {
//        YMListView()
//    }
//}
